package pso.tsp;

import java.io.*;
import java.util.ArrayList;

import static java.lang.Float.valueOf;

/**
 * @Author: 陶鹏飞
 * @Description: 城市单例
 * @Date: 2018/9/10-14:13
 * @email: taopengfei1223@foxmail.com
 **/
public class CityLab {
    private static CityLab mCityLab = null;
    private ArrayList<City> mCities = new ArrayList<>();

    private CityLab() {
        //读取文件，添加城市信息到mcities中
        String filename = "E://Dataguru.Learning.Course//软件架构必备基础（第四期）//10.聊聊分布式系统与计算//粒子群求解TSP问题（JAVA）//city.csv";
        String strbuff;
        BufferedReader data = null;
        try {
            data = new BufferedReader(new InputStreamReader(
                    new FileInputStream(filename)));
            int id = 1;
            while (true) {
                //读取一行数据:北京,116.41667,39.91667
                strbuff = data.readLine();
                if (strbuff == null) {
                    break;
                }
                String[] arr = strbuff.split(",");
                City c = new City(id, arr[1], valueOf(arr[2]), valueOf(arr[3]));
                id++;
                mCities.add(c);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static CityLab getInstance() {
        if (mCityLab == null) {
            mCityLab = new CityLab();
        }
        return mCityLab;
    }

    public ArrayList<City> getmCities() {
        return mCities;
    }
}
